package com.darsystems.foghourz.server;

import java.util.logging.Logger;

import com.darsystems.foghourz.client.GoogleLoginInfo;
import com.darsystems.foghourz.client.GoogleLoginService;
import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;

public class GoogleLoginServiceImpl extends RemoteServiceServlet implements
		GoogleLoginService {

	private final static Logger log = Logger.getLogger(GoogleLoginServiceImpl.class.getName());
	/**
	 * 
	 */
	private static final long serialVersionUID = -3488573045623599647L;

	public GoogleLoginInfo login(String requestUri) {
	    UserService userService = UserServiceFactory.getUserService();
	    User user = userService.getCurrentUser();
	    GoogleLoginInfo loginInfo = new GoogleLoginInfo();

	    if (user != null) {
	      loginInfo.setLoggedIn(true);
	      loginInfo.setEmailAddress(user.getEmail());
	      loginInfo.setNickname(user.getNickname());
	      loginInfo.setLogoutUrl(userService.createLogoutURL(requestUri));
	    } else {
	      loginInfo.setLoggedIn(false);
	      loginInfo.setLoginUrl(userService.createLoginURL(requestUri));
	    }
	    return loginInfo;
	  }

}
